package com.jingdianjichi.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.base.Preconditions;
import com.jingdianjichi.application.controller.convert.AuthUserDTOConvert;
import com.jingdianjichi.auth.domain.bo.AuthUserBO;
import com.jingdianjichi.auth.domain.convert.AuthUserBOConvert;
import com.jingdianjichi.auth.domain.service.AuthUserDomainService;;
import com.jingdianjichi.auth.entity.AuthUserDTO;
import com.jingdianjichi.auth.entity.Result;
import com.jingdianjichi.auth.infra.bastic.entity.AuthUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName:UserController
 * Package:com.jingdianjichi.application.controller
 * Description:
 *
 * @Author fst
 * @Create 2024/10/8 20:2
 * @Version 1.0
 */
@RestController
@RequestMapping("/user/")
@Slf4j
public class AuthUserController
{
    @Resource
    private AuthUserDomainService authUserDomainService;

    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam("validCode") String validCode)
    {
        Preconditions.checkArgument(!StringUtils.isEmpty(validCode),"验证码不可以为空");
      SaTokenInfo saTokenInfo=authUserDomainService.doLogin(validCode);
      return Result.ok(saTokenInfo);
    }
    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    @PostMapping("register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO)
    {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthUserController.register.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getUserName(),"name不能为空");
            Preconditions.checkNotNull(authUserDTO.getPassword(),"密码不能为空");
            Preconditions.checkNotNull(authUserDTO.getNickName(),"NickName不能为空");

            AuthUserBO authUserBO = AuthUserDTOConvert.INSTANDE.convertDTOTOBOUser(authUserDTO);



            authUserDomainService.add(authUserBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("AuthUserController.register.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO)
    {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthUserController.update.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getUserName(),"name不能为空");
            Preconditions.checkNotNull(authUserDTO.getNickName(),"NickName不能为空");

            AuthUserBO authUserBO = AuthUserDTOConvert.INSTANDE.convertDTOTOBOUser(authUserDTO);

            authUserDomainService.update(authUserBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("AuthUserController.update.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO)
    {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthUserController.delete.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getId(),"id不能为空");

            AuthUserBO authUserBO = AuthUserDTOConvert.INSTANDE.convertDTOTOBOUser(authUserDTO);

            authUserDomainService.delete(authUserBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("AuthUserController.delete.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO)
    {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthUserController.changeStatus.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getId(),"id不能为空");
            Preconditions.checkNotNull(authUserDTO.getStatus(),"Status不能为空");

            AuthUserBO authUserBO = AuthUserDTOConvert.INSTANDE.convertDTOTOBOUser(authUserDTO);

            authUserDomainService.update(authUserBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("AuthUserController.changeStatus.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO)
    {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthUserController.changeStatus.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getUserName(),"用户名不能为空");

            AuthUserBO authUserBO = AuthUserDTOConvert.INSTANDE.convertDTOTOBOUser(authUserDTO);

            authUserBO=authUserDomainService.getUserInfo(authUserBO);

            AuthUser authUser= AuthUserBOConvert.INSTANCE.convertBOTOUser(authUserBO);

            return Result.ok(authUser);
        } catch (Exception e) {
            log.error("AuthUserController.changeStatus.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }



    @RequestMapping("logOut")
    public Result<AuthUserDTO> getUserInfo(@RequestParam String userName)
    {
        try {
           log.info("UserController.logOut.userName:{}",userName);
            Preconditions.checkNotNull(userName,"用户名不能为空");

            StpUtil.logout(userName);

            return Result.ok();
        } catch (Exception e) {
            log.error("UserController.logOut.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }



    @PostMapping("listByIds")
    public Result<List<AuthUserDTO>> listByIds(@RequestBody List<String> userNameList)
    {
        try {
            if(log.isInfoEnabled())
            {
                log.info("UserController.listUserInfoByIds.dto:{}",JSON.toJSONString(userNameList));
            }

            Preconditions.checkArgument(!CollectionUtils.isEmpty(userNameList),"用户名不能为空");
            List<AuthUserBO> authUserBOList=authUserDomainService.listUserInfoByIds(userNameList);
            return Result.ok(AuthUserDTOConvert.INSTANDE.convertBOToDTOList(authUserBOList));

        } catch (Exception e)
        {
            log.error("UserController.listUserInfoByIds.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }


}
